package org.csu.mybigpro.controller;

import org.csu.mybigpro.common.CommonResponse;
import org.csu.mybigpro.domain.User;
import org.csu.mybigpro.service.LiveKitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/live")
public class LiveKitController {

    @Autowired
    private LiveKitService liveKitService;

    /**
     * 用于接收前端请求的内部类，需要包含 role
     */
    private static class JoinRequest {
        public String roomName;
        public String participantName;
        public String role; // 'publisher' 或 'subscriber'
    }

    @PostMapping("/join-token")
    public CommonResponse<Map<String, String>> getJoinToken(@RequestBody JoinRequest request) {
        User user = new User();
        user.setId(System.currentTimeMillis() % 100000);
        user.setUsername(request.participantName);

        // 将 role 传递给 LiveKitService
        String token = liveKitService.getJoinToken(request.roomName, user, request.role);

        Map<String, String> responseData = new HashMap<>();
        responseData.put("token", token);

        return CommonResponse.createForSuccess("Token generated successfully", responseData);
    }
}
